<template>
	<view class="container">
		<u-form ref="uForm" :rules="rules" :model="formDatas" label-width="220rpx">
		
			<!-- 姓名 -->
			<u-form-item label="姓名" prop="name">
				<u-input v-model="formDatas.name" :disabled="!isEdit"></u-input>
			</u-form-item>
		
		
			<!-- 电话 -->
			<u-form-item label="电话" prop="phone">
				<u-input v-model="formDatas.phone" :disabled="!isEdit"></u-input>
			</u-form-item>
		
			<u-button :ripple="true" @click="onSubmit(formDatas)" type="primary" :loading="loading">提交</u-button>
		</u-form>
		
		<u-toast ref="uToast"/>
	</view>
</template>

<script>
	import {
		register,
		wxLogin
	} from "@/api/apiList/user.js"
	export default {
		data() {
			return {
				loading: false,
				isTab:true,
				isEdit: {
					//是否为编辑状态
					type: Boolean,
					default: true,
				},
				formDatas: {
					name: "", //姓名
					phone: "", //电话号码
				},
				showGdtm: false, //显示毕业弹窗
				showBirth: false, //显示生日弹窗
				rules: {
					//验证规则
					name: [{
						required: true,
						trigger: "blur",
						message: "请输入姓名",
					}, ],
					phone: [{
							required: true,
							trigger: "blur",
							message: "请输入电话号码",
						},
						{
							trigger: "blur",
							pattern: /^[1][3,4,5,7,8][0-9]{9}$/,
							message: "请输入正确的手机号码",
						},
					],
				},
			}
		},
		methods: {
			async API_register(data) {
				
				let result = {
					data, //更新数据
				};
				return await register(result);
			},
			async API_wxLogin(unionid) { //登录
				let result = {
					param: {
						unionid
					}
				}
				return await wxLogin(result)
			},
			onSubmit(formData) {
				
				this.$refs.uForm.validate(async valid => {
					if (valid) {
						this.loading = true;
						
						let newFormData = Object.assign({}, formData, {
							wxuid: uni.getStorageSync("unionId"),
							openid: uni.getStorageSync("openid"),
						})
						
						let res = await this.API_register(newFormData);

						if (res.data.code === 1000) {
							
							let loginRes = await this.API_wxLogin(uni.getStorageSync("unionId"))
							
							if (loginRes.data.code === 1000) {
								uni.setStorageSync("jwttoken", loginRes.data.data.jwttoken) //存token
								this.$refs.uToast.show({
									title: loginRes.data.msg,
									type: 'success',
									url: '/pages/home/home',
									isTab:true
								})
							}else{
								this.$refs.uToast.show({
									title: loginRes.data.msg,
									type: 'error',
								})
							}
						}
						this.loading = false;
					} else {
						console.log('验证失败');
					}
				})
			}
		}
	}
</script>

<style>

</style>
